/*
 * @Date: 2020-07-15 17:14:28
 * @LastEditTime: 2020-07-15 17:44:56
 * @Description: 严格模式计算类
 * @FilePath: \vue\src\common\utilCalc.js
 */ 

"use strict"
class Calculate {
  add(v1, v2) {
    //<summary>精确计算加法。语法：Calculate.add(v1, v2)</summary>
    //<param name="v1" type="number">操作数。</param>
    //<param name="v2" type="number">操作数。</param>
    //<returns type="number">计算结果。</returns>
    var r1, r2, m;
    try {
      r1 = v1.toString().split(".")[1].length;
    }
    catch (e) {
      r1 = 0;
    }
    try {
      r2 = v2.toString().split(".")[1].length;
    }
    catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));

    return (v1 * m + v2 * m) / m;
  }

  sub(v1, v2) {
    return this.add(v1, -v2);
  }

  mul(v1, v2) {
    //<summary>精确计算乘法。语法：Calculate.mul(v1, v2)</summary>
    //<param name="v1" type="number">操作数。</param>
    //<param name="v2" type="number">操作数。</param>
    //<returns type="number">计算结果。</returns>
    var m = 0;
    var s1 = v1.toString();
    var s2 = v2.toString();
    try {
      m += s1.split(".")[1].length;
    }
    catch (e) {
    }
    try {
      m += s2.split(".")[1].length;
    }
    catch (e) {
    }

    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  }

  div(v1, v2) {
    //<summary>精确计算除法。语法：Calculate.div(v1, v2)</summary>
    //<param name="v1" type="number">操作数。</param>
    //<param name="v2" type="number">操作数。</param>
    //<returns type="number">计算结果。</returns>
    var t1 = 0;
    var t2 = 0;
    var r1, r2;
    try {
      t1 = v1.toString().split(".")[1].length;
    }
    catch (e) {
    }
    try {
      t2 = v2.toString().split(".")[1].length;
    }
    catch (e) {
    }

    r1 = Number(v1.toString().replace(".", ""));
    r2 = Number(v2.toString().replace(".", ""));
    return (r1 / r2) * Math.pow(10, t2 - t1);
  }
}

export default new Calculate()


